package com.epay;

import java.io.File;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class CreaterBean {

	public static boolean createrBean(ResultSet rs ){
		try{
			 String workpath = Constants.workpath;
			String tableNameLower=Constants.fileName+"DO";
			String tableName=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1);
			  Map<String, String> map=new LinkedHashMap<String, String>();
			 //创建bean
			 String dir = workpath + File.separator + CreaterUtil.replaceAll(Constants.searchModel_package, ".", File.separator);
		     File dirfile = new File(dir);
		     if (!dirfile.exists()) {
		       dirfile.mkdirs();
		     }
		     
		     File beanfile = new File(dir+"/" + tableName+".java");
		     FileWriter fw2 = new FileWriter(beanfile);
		     fw2.write("package " + Constants.searchModel_package + ";\n");
		     
		     fw2.write("import com.epay.common.base.BaseEntity;\n");
		     fw2.write("\n");
		     fw2.write("/**\n");
			 fw2.write(" * Entity\n");
		     fw2.write(" * @author Epay\n");
		     fw2.write(" * @date "+Constants.getSysDate()+"\n");
		     fw2.write(" */\n");
		     fw2.write("public class " + tableName + " extends BaseEntity<"+tableName+">{");
		     fw2.write("\n");

		     while (rs.next()) {
		    	 String name=CreaterUtil.CapitalAsLowerCase(rs.getString("column_name"));
			     String dataType=rs.getString("data_type");
			     String columnComment=rs.getString("column_comment");
				 System.out.println("column_name: " + rs.getString("column_name"));
				 //根据数据库类型获取java类型     "java.lang.String"
				 String type=CreaterUtil.getDbforJavaType(dataType);
				 map.put(name, type);
				 Map<String,String> pamertMap=new HashMap<String,String>();
				 pamertMap.put("name", name);
				 pamertMap.put("dataType", dataType);
				 pamertMap.put("columnComment", columnComment);
				 Constants.objMap.put(rs.getString("column_name"), pamertMap);
				 if(!"id".equals(name)&&!"createTime".equals(name)&&!"updateTime".equals(name)) {
				 fw2.write("\t/**"+columnComment+"*/\n");
					 fw2.write("\tprivate " + type + " " + name + ";\n");
				 }
	      		
	         }

		    /* //构造方法
		     StringBuilder name=new StringBuilder();
		     StringBuilder value=new StringBuilder();
		     fw2.write("\tpublic " + tableName + "(){\n");
		     fw2.write("\t}\n");

		     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				String name1=String.valueOf(e.getKey());
				//String name2=String.valueOf(e.getKey()).substring(0,1).toUpperCase()+String.valueOf(e.getKey()).substring(1);
				String type=String.valueOf(e.getValue());
				 name.append(type+" "+name1+",");
				 value.append("\t\tthis."+name1+" = "+name1+";\n");
		     }
			fw2.write("\n");
			fw2.write("\tpublic " + tableName + "("+name.toString().substring(0,name.length()-1)+"){\n");
			fw2.write(value.toString());
		     fw2.write("\t}\n");*/

		     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				 Map.Entry e = (Map.Entry) it.next();
			     String name1=String.valueOf(e.getKey());
			     String name2=String.valueOf(e.getKey()).substring(0,1).toUpperCase()+String.valueOf(e.getKey()).substring(1);
			     String type=String.valueOf(e.getValue());
				 if(!"id".equals(name1)&&!"createTime".equals(name1)&&!"updateTime".equals(name1)) {
					 fw2.write("\n");
					 fw2.write("\tpublic " + type + " get" + name2 + "(){\n");
					 fw2.write("\t\treturn " + name1 + ";\n");
					 fw2.write("\t}\n");
					 fw2.write("\n");
					 fw2.write("\tpublic void set" + name2 + "(" + type + " " + name1 + "){\n");
					 fw2.write("\t\tthis." + name1 + "=" + name1 + ";\n");
					 fw2.write("\t}\n");
				 }
			  }

			 fw2.write("}\n");
			 fw2.close();
			 
		}catch(Exception e){
			System.out.println("创建bean异常");
			e.printStackTrace();
		}
		return true;
	}
}
